#include "power.h"

extern volatile bool wakeup_flag;
extern volatile u16 value[NUM_SAM];
extern volatile u16 res;
extern volatile u8 samp_counter;
extern volatile u8 ADC_data_ready;
extern volatile bool User_Key_Pressed;
extern volatile bool FORCED_CALIB;
extern volatile u16 Sec2_count;

void ADC_Periph_Init(void)
{
    // 使能ADC1时钟
    CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE);
    // ADC1->CR1 |= 0x01; //ADON
    // ADC1->TRIGR[0] = 0x20; //TSON
    // for (idx = 0;idx < 50;idx++);
    ADC_Init(ADC1, ADC_ConversionMode_Single,
            ADC_Resolution_12Bit, ADC_Prescaler_1);
    ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_24Cycles);
    ADC_SamplingTimeConfig(ADC1, ADC_Group_FastChannels, ADC_SamplingTime_24Cycles);
    ADC_Cmd(ADC1, ENABLE);
    ADC_VrefintCmd(ENABLE);
    ADC_ChannelCmd(ADC1, ADC_Channel_Vrefint, ENABLE);
    ADC_ChannelCmd(ADC1, ADC_Channel_3, ENABLE);
    // ADC1->SQR[0] |= 0x80; //DMA disable
}

void DMA_Config(void)
{
    CLK_PeripheralClockConfig(CLK_Peripheral_DMA1, ENABLE);
    SYSCFG_REMAPDMAChannelConfig(REMAP_DMA1Channel_ADC1ToChannel0);
    DMA_Init(DMA1_Channel0, BUFFER_ADDRESS, ADC1_DR_ADDRESS, BUFFER_SIZE, DMA_DIR_PeripheralToMemory,
            DMA_Mode_Circular, DMA_MemoryIncMode_Inc, DMA_Priority_High, DMA_MemoryDataSize_HalfWord);
    DMA_Cmd(DMA1_Channel0, ENABLE);
    DMA_ITConfig(DMA1_Channel0, DMA_ITx_TC, ENABLE);
    DMA_GlobalCmd(ENABLE);
    ADC_DMACmd(ADC1, ENABLE);
}

void RTC_Periph_Init(void)
{
    CLK_PeripheralClockConfig(CLK_Peripheral_RTC, ENABLE);
#ifdef USE_LSE
    CLK_RTCClockConfig(CLK_RTCCLKSource_LSE, CLK_RTCCLKDiv_1);
#else
    CLK_RTCClockConfig(CLK_RTCCLKSource_LSI, CLK_RTCCLKDiv_1);
#endif

    /* Configures the RTC wakeup timer_step = RTCCLK/16 = LSE/16 = 488.28125 us */
    RTC_WakeUpClockConfig(RTC_WakeUpClock_CK_SPRE_16bits);
    /* Enable wake up unit Interrupt */
    RTC_ITConfig(RTC_IT_WUT, ENABLE);
    RTC_SetWakeUpCounter(5);
    RTC_WakeUpCmd(ENABLE);
  //   RTC_InitStr.RTC_AsynchPrediv = 0x7f;
  //   RTC_InitStr.RTC_SynchPrediv = 0x00ff;
  //   RTC_Init(&RTC_InitStr);
}

void RTC_restart(void)
{
    RTC_SetWakeUpCounter(5);
    RTC_WakeUpCmd(ENABLE);
}

/**
  * @brief DMA1 channel0 and channel1 Interrupt routine.
  * @par Parameters:
  * None
  * @retval
  * None
  */
INTERRUPT_HANDLER(DMA1_CHANNEL0_1_IRQHandler, 2)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
Verfin = (uint32_t)1224*4096/Buffer[1];
DMA_ClearITPendingBit(DMA1_IT_TC0);
}

/**
  * @brief RTC Interrupt routine.
  * @par Parameters:
  * None
  * @retval
  * None
  */

INTERRUPT_HANDLER(RTC_IRQHandler, 4)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
//  while (1);

  //if (((RTC->ISR2) & 0x4) == 0x4)
  {
    RTC_ClearITPendingBit(RTC_IT_WUT);
    // RTC->ISR1 = 0x0;
    // RTC->ISR2 = 0x0;
    // ADC_TempSensorCmd(ENABLE);
    wakeup_flag = TRUE;
  }
}

/**
  * @brief ADC1/Comparator Interrupt routine.
  * @par Parameters:
  * None
  * @retval
  * None
  */
INTERRUPT_HANDLER(ADC1_COMP_IRQHandler, 18)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
  //while (1);
  value[samp_counter] = ADC_GetConversionValue(ADC1);
  //res += value[samp_counter];
  if (++samp_counter >= NUM_SAM)
  {
    samp_counter = 0;
    ADC_data_ready = TRUE;
  }
  return;
}


#if 0
volatile u16 value[NUM_SAM];
volatile u8 samp_counter = 0;

 disableInterrupts();
    // EEPROM_Initialize();
    // EEPROM_Write_Byte(0x00, 100);
    // EEPROM_Write_Byte(0x01, 200);
    // uint8_t ay = EEPROM_Read_Byte(0x00);
    // uint8_t by = EEPROM_Read_Byte(0x01);
//   EXTI_DeInit();
    Magnetic_Initialize();
    Usart1_Initialize();
    Timer2_Initialize(5);
    Timer2_Start();
//   RTC_Periph_Init();
//   EXTI_SetPinSensitivity(EXTI_Pin_0, EXTI_Trigger_Falling);
//   ITC_SetSoftwarePriority(EXTI0_IRQn,ITC_PriorityLevel_1);

    CLK_RTCClockConfig(CLK_RTCCLKSource_LSI, CLK_RTCCLKDiv_1);
    CLK_PeripheralClockConfig(CLK_Peripheral_RTC, ENABLE);
    /* Configures the RTC wakeup timer_step = RTCCLK/16 = LSE/16 = 488.28125 us */
    //RTC_WakeUpClockConfig(RTC_WakeUpClock_CK_SPRE_16bits);
    /* Enable wake up unit Interrupt */
    //RTC_ITConfig(RTC_IT_WUT, ENABLE);
    //ADC_Periph_Init();
    //DMA_Config();
    Magnetic_SetEnable(TRUE);
    enableInterrupts();
    //ADC_SoftwareStartConv(ADC1);
    while (1) {
    //  delay_ms(6000);
        // delay_1us(25); // TS startup time
        // ADC_SoftwareStartConv(ADC1);
        // while (!(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC)));
        // value[samp_counter] = ADC_GetConversionValue(ADC1);
        // yang = (uint32_t)1225*4096/value[samp_counter];
        // RTC_restart();
        // RTC_SetWakeUpCounter(5);
        // GPIO_ResetBits(GPIOB, GPIO_Pin_1);
        // RTC_WakeUpCmd(ENABLE);
        // halt();
        // RTC_WakeUpCmd(DISABLE);
    //  state = GPIO_ReadInputDataBit(MAGNETIC_PORT, MAGNETIC_PIN);
    }
}
#endif